package cn.gailvlun.gll.widget.edittext

import android.text.InputFilter
import android.text.SpannableStringBuilder
import android.text.Spanned
import cn.gailvlun.gll.util.LogUtil


abstract class BaseFilter : InputFilter {

    abstract fun isAllowed(c: Char): Boolean
    
    override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
        var modification: SpannableStringBuilder? = null
        var modoff = 0
        
        
        for (i in start until end) {
            val c: Char = source[i]
            if (isAllowed(c)) {
                modoff++
            } else {

                if (modification == null) {
                    modification = SpannableStringBuilder(source, start, end)
                    modoff = i - start
                }
                modification.delete(modoff, modoff + 1)
            }
        }

        return modification
    }
}